package com.codedancer.campus.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.client.service.ILostFoundItemCommentService;
import com.codedancer.campus.client.service.bo.lostFoundItemComment.LostFoundCommentPageSearchBo;
import com.codedancer.campus.client.service.bo.lostFoundItemComment.LostFoundItemCommentAddBo;
import com.codedancer.campus.client.service.vo.lostFoundItemComment.LostFoundItemCommentVo;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.enums.LostFoundItemStatus;
import com.codedancer.campus.mapper.LostFoundItemCommentMapper;
import com.codedancer.campus.mapper.LostFoundItemMapper;
import com.codedancer.campus.mapper.UserMapper;
import com.codedancer.campus.po.LostFoundItem;
import com.codedancer.campus.po.LostFoundItemComment;
import com.codedancer.campus.po.User;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class LostFoundItemCommentServiceImpl implements ILostFoundItemCommentService {

    @Autowired
    private LostFoundItemCommentMapper lostFoundItemCommentMapper;
    @Autowired
    private LostFoundItemMapper lostFoundItemMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加评论
     *
     * @param lostFoundItemCommentAddBo
     */
    @Override
    public String add(LostFoundItemCommentAddBo lostFoundItemCommentAddBo) {
        LostFoundItem lostFoundItem = lostFoundItemMapper.findById(
            lostFoundItemCommentAddBo.getLostFoundItemUuid())
            .orElseThrow(() -> new BusinessException("物品不存在"));
        if (lostFoundItem.getStatus().equals(LostFoundItemStatus.PENDING_REVIEW)) {
            throw new BusinessException("该物品还未审核，请联系管理员");
        }
        if (lostFoundItem.getStatus().equals(LostFoundItemStatus.NOT_PASS)) {
            throw new BusinessException("该物品未通过审核，请联系管理员");
        }
        if (lostFoundItem.getStatus().equals(LostFoundItemStatus.RECALL)) {
            throw new BusinessException("该物品已撤回，请联系管理员");
        }
        LostFoundItemComment lostFoundItemComment = BeanUtil.copyProperties(
            lostFoundItemCommentAddBo, LostFoundItemComment.class);
        lostFoundItemComment.setCommentUserUuid(lostFoundItemCommentAddBo.getCurrentUserUuid());
        lostFoundItemComment.setUuid(UUIDGenerator.getUUID());
        lostFoundItemComment.setReplayUserUuid(lostFoundItem.getPublishUserUuid());
        lostFoundItemCommentMapper.insert(lostFoundItemComment);
        return lostFoundItemComment.getUuid();
    }

    /**
     * 分页查询某个物品的评论
     *
     * @param lostFoundCommentPageSearchBo
     * @return
     */
    @Override
    public IPage<LostFoundItemCommentVo> page(
        LostFoundCommentPageSearchBo lostFoundCommentPageSearchBo) {
        IPage<LostFoundItemComment> lostFoundItemCommentIPage = lostFoundItemCommentMapper.page(
            new Page<LostFoundItemComment>(lostFoundCommentPageSearchBo.getPage(),
                lostFoundCommentPageSearchBo.getSize()),
            lostFoundCommentPageSearchBo.getLostFoundItemUuid());
        IPage<LostFoundItemCommentVo> lostFoundItemCommentVoIPage = new Page<>();
        List<LostFoundItemCommentVo> lostFoundItemCommentVos;
        lostFoundItemCommentVos = lostFoundItemCommentIPage.getRecords().stream()
            .map(lostFoundItemComment -> {
                LostFoundItemCommentVo lostFoundItemCommentVo = BeanUtil.copyProperties(
                    lostFoundItemComment, LostFoundItemCommentVo.class);
                User user = userMapper.selectById(lostFoundItemComment.getCommentUserUuid());
                lostFoundItemCommentVo.setCommentLoginName(user.getNickName());
                lostFoundItemCommentVo.setCommentUserAvatar(user.getAvatar());
                return lostFoundItemCommentVo;
            }).collect(Collectors.toList());
        lostFoundItemCommentVoIPage.setTotal(lostFoundItemCommentIPage.getTotal());
        lostFoundItemCommentVoIPage.setRecords(lostFoundItemCommentVos);
        return lostFoundItemCommentVoIPage;
    }
}
